package net.xxcxcxcx.xc_enhancedmob.config.render;

import com.google.gson.annotations.Expose;
import net.minecraft.util.Formatting;
import net.xxcxcxcx.xc_enhancedmob.config.BaseConfig;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 构建一个config类，用来记录可被自定义的诸多数据
 * <p>json结构与class的包含结构对应
 */
public class RenderConfig extends BaseConfig {
    @Expose(deserialize = false)
    public String message = getMessage();
    @Expose
    public List<DamageNumConfig> DamageNumConfigs = BuildColors();
    public Map<String, DamageNumConfig> DamageNumConfigMap = DamageNumConfigs.stream().collect(Collectors.toMap(DamageNumConfig::getKey, f -> f));

    public Map<String, DamageNumConfig> DefaultDamageNumConfigMap = DamageNumConfigs.stream().collect(Collectors.toMap(DamageNumConfig::getKey, f -> f));

    public RenderConfig() {
        setName("RenderConfig");
        setFilename("Render/");
    }

    public String getMessage() {
        String s = "伤害和治疗的颜色设定,可以填:";
        int size = Formatting.values().length;
        for (int i = 0; i < size; i++) {
            s += Formatting.values()[i].name();
            if (i != size - 1) s += ",";
        }
        return s;
    }

    //伤害类型配置文件
    public List<DamageNumConfig> BuildColors() {
        List<DamageNumConfig> colorList = new ArrayList<>();
        colorList.add(new DamageNumConfig("Default", "默认", Formatting.RED));
        colorList.add(new DamageNumConfig("Heal", "治疗", Formatting.GREEN, 2, 0));
        colorList.add(new DamageNumConfig("inFire", "在火里", Formatting.RED));
        colorList.add(new DamageNumConfig("lightningBolt", "闪电", Formatting.RED));
        colorList.add(new DamageNumConfig("onFire", "着火", Formatting.RED));
        colorList.add(new DamageNumConfig("lava", "岩浆", Formatting.RED));
        colorList.add(new DamageNumConfig("hotFloor", "热地板", Formatting.RED));
        colorList.add(new DamageNumConfig("inWall", "窒息", Formatting.RED));
        colorList.add(new DamageNumConfig("cramming", "挤压", Formatting.RED));
        colorList.add(new DamageNumConfig("drown", "碾压", Formatting.RED));
        colorList.add(new DamageNumConfig("starve", "饥饿", Formatting.RED));
        colorList.add(new DamageNumConfig("cactus", "仙人掌", Formatting.RED));
        colorList.add(new DamageNumConfig("fall", "摔落伤害", Formatting.RED));
        colorList.add(new DamageNumConfig("flyIntoWall", "撞墙", Formatting.RED));
        colorList.add(new DamageNumConfig("outOfWorld", "掉出世界", Formatting.RED));
        colorList.add(new DamageNumConfig("generic", "通用", Formatting.RED));
        colorList.add(new DamageNumConfig("magic", "魔法", Formatting.RED));
        colorList.add(new DamageNumConfig("wither", "凋零", Formatting.RED));
        colorList.add(new DamageNumConfig("anvil", "铁毡", Formatting.RED));
        colorList.add(new DamageNumConfig("fallingBlock", "掉落方块", Formatting.RED));
        colorList.add(new DamageNumConfig("dragonBreath", "龙息", Formatting.RED));
        colorList.add(new DamageNumConfig("dryout", "变干", Formatting.RED));
        colorList.add(new DamageNumConfig("sweetBerryBush", "甜莓丛", Formatting.RED));
        colorList.add(new DamageNumConfig("mob", "生物", Formatting.RED));
        colorList.add(new DamageNumConfig("player", "玩家", Formatting.RED));
        colorList.add(new DamageNumConfig("arrow", "箭矢", Formatting.RED));
        colorList.add(new DamageNumConfig("trident", "三叉戟", Formatting.RED));
        colorList.add(new DamageNumConfig("fireworks", "烟花", Formatting.RED));
        colorList.add(new DamageNumConfig("fireball", "火球", Formatting.RED));
        colorList.add(new DamageNumConfig("witherSkull", "凋零头", Formatting.RED));
        colorList.add(new DamageNumConfig("thrown", "投掷物", Formatting.RED));
        colorList.add(new DamageNumConfig("indirectMagic", "间接的魔法", Formatting.RED));
        colorList.add(new DamageNumConfig("thorns", "荆棘", Formatting.RED));
        colorList.add(new DamageNumConfig("explosion.player", "爆炸玩家", Formatting.RED));
        colorList.add(new DamageNumConfig("explosion", "爆炸", Formatting.RED, 3, 0));
        colorList.sort(Comparator.comparing(DamageNumConfig::getKey));
        return colorList;
    }

    @Override
    public void RepairData() {
        super.RepairData();
        for (int i = 0; i < DamageNumConfigs.size(); i++) {
            DamageNumConfig damageNumConfig = DamageNumConfigs.get(i);
            if (damageNumConfig.getKey().equals("Default")) {
                DamageNumConfigs.set(i, DefaultDamageNumConfigMap.get("Default"));
            }
        }
        for (DamageNumConfig damageNumConfig : DamageNumConfigs) {
            if (DefaultDamageNumConfigMap.get(damageNumConfig.getKey()) != null) {
                damageNumConfig.setName(DefaultDamageNumConfigMap.get(damageNumConfig.getKey()).getName());
            }
            if (damageNumConfig.getColor() == null) {
                if (DefaultDamageNumConfigMap.get(damageNumConfig.getKey()) != null) {
                    damageNumConfig.setColor(DefaultDamageNumConfigMap.get(damageNumConfig.getKey()).getColor());
                } else {
                    damageNumConfig.setColor(DefaultDamageNumConfigMap.get("Default").getColor());
                }
            }
        }
        DamageNumConfigMap = DamageNumConfigs.stream().collect(Collectors.toMap(DamageNumConfig::getKey, f -> f));
        Set<String> strings = DefaultDamageNumConfigMap.keySet();
        strings.forEach(s -> {
            if (DamageNumConfigMap.get(s) == null) {
                DamageNumConfigMap.put(s, DefaultDamageNumConfigMap.get(s));
                DamageNumConfigs.add(DefaultDamageNumConfigMap.get(s));
            }
        });
        DamageNumConfigs.sort(Comparator.comparing(DamageNumConfig::getKey));
    }

    public DamageNumConfig getDamageNumConfig(String s) {
        if (DamageNumConfigMap.get(s) != null) {
            return DamageNumConfigMap.get(s);
        }
        return DamageNumConfigMap.get("Default");
    }

}
